package services

import config.{Global, MyAwait, MyFile, MyRow}
import dao.{CmDao, CsDao, GenomeDao}
import javax.inject.{Inject, Singleton}
import play.api.libs.json.Json
import utils.TableUtils

import scala.concurrent.ExecutionContext

@Singleton
class OnStart @Inject()(csDao: CsDao, cmDao: CmDao, genomeDao: GenomeDao)(implicit exec: ExecutionContext) extends MyFile with MyRow with MyAwait {

  // TableUtils.cs = csDao.getAllGene.toAwait.map(_.getMapByT())

  // println("[Info] Cs info loading success !")

  // TableUtils.cm = cmDao.getAllGene.toAwait.map(_.getMapByT())

  // println("[Info] Cm info loading success !")

  TableUtils.genome = genomeDao.getAllGenome.toAwait.map(_.getMapByT())

  val line = s"${Global.path}/QTLs.txt".readLines.map(_.split("\t"))
  val header = line.head.map(_.replaceAll("\"", ""))
  val field = header.map(_.split(" ").mkString("_"))

  println(field.mkString(","))
  TableUtils.qtlHeader = header.map{x=>
    Json.obj("field" -> x.split(" ").mkString("_") , "title" -> x)
  }

  TableUtils.qtlStat = line.tail.map { x =>
    x.zipWithIndex.map { case (y, i) =>
      field(i) -> y
    }.toMap
  }.toSeq

  TableUtils.hypgeomdist = s"${Global.path}/hypgeomdist.txt".readLines.tail.map(_.split("\t")).map{x=>
    x.zipWithIndex.map(y=>y._2.toString -> y._1).toMap
  }.toSeq

}
